# coding=utf-8
"""
author: jamon
"""
from typing import Optional

from client.model.weapon.bullet import Bullet
from client.model.gate_data import GateData
from package.posi import Position
from package.static.const import TypeIdConst, Faction, MoveDirection
from package.universe import universe


class Weapon(object):
    """
    武器
    """

    def __init__(self, type_id: TypeIdConst, faction: Faction):
        self._type_id = type_id
        self.cool_time: int = 0
        self.bullet_id: TypeIdConst = universe.get_weapon_special_info(type_id).bullet
        self.faction: Faction = faction

    def tick_by_per_frame(self):
        self.cool_time = max(0, self.cool_time - 1)

    def __str__(self):
        return "<{} bullet_id={} cool_time={}>".format(self.__class__.__name__, self.bullet_id, self.cool_time)

    __repr__ = __str__

    def reset_cool_time(self):
        self.cool_time = int(universe.get_weapon_special_info(self._type_id).cool_time * GateData().frame_physical_rate)

    def shoot(self, start_position: Position, direction: MoveDirection) -> Optional[Bullet]:
        if not self.cool_time:
            # 不在冷却中，可以开火
            bullet = Bullet(self.bullet_id, self.faction, start_position, direction)
            self.reset_cool_time()
            return bullet
        return
